package com.study_platform.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study_platform.entity.User;
import com.study_platform.service.UserService;
import com.study_platform.util.VO.LoginInfo;
import com.study_platform.util.VO.RegisterInfoVO;

import com.study_platform.util.VO.UpdateInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author auto
 * @since 2023-04-11
 */
@RestController
@RequestMapping("/study_platform/user")
public class UserController {
    @Resource
    private UserService userService;

    @GetMapping("/powerhouses/")
    public JSONObject getPower(){
        List<User> users = userService.lambdaQuery()
                .orderByDesc(User::getRating)
                .page(new Page<>(1, 10))
                .getRecords();

        for (User user : users
             ) {
            user.setPassword(null);
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("users", users);
        return jsonObject;
    }

    @PostMapping("/account/register/")
    public JSONObject register(@RequestBody RegisterInfoVO registerInfo) {
        System.out.println("registerInfo = " + registerInfo);
        return userService.register(registerInfo);
    }

    @GetMapping("/account/login/")
    public JSONObject getInfo() {
        return userService.getInfo();
    }


    @PostMapping("/account/login/")
    public JSONObject login(@RequestBody LoginInfo loginInfo) {
        System.out.println(loginInfo);
        return userService.login(loginInfo);
    }
    @PostMapping("/account/update/")
    public JSONObject update(@RequestBody UpdateInfo updateInfo){
        return userService.updateInfo(updateInfo);
    }

    @GetMapping("/account/ranklist/")
    public List<User> getRankList(){
        List<User> users = userService.list(new QueryWrapper<User>().orderByDesc("rating"));
        for (User user : users) {
            user.setPassword("");
        }
        return users;
    }
}

